﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace PloobsEngine.SceneControl
{
    public enum FogType
    {
        LINEAR,EXPONENCIAL,EXPONENCIALSQUARED
    }

    public class FogPostEffect : IPostEffect
    {
        /// <summary>
        /// Linear
        /// </summary>
        /// <param name="nearPlane"></param>
        /// <param name="farPlane"></param>
        public FogPostEffect(float nearPlane,float farPlane)
        {
            this.farPlane = farPlane;
            this.nearPlane = nearPlane;
            fogType = FogType.LINEAR;
        }

        /// <summary>
        /// Exponencial or Exponencial Squared
        /// </summary>
        /// <param name="density"></param>
        /// <param name="type"></param>
        public FogPostEffect(float density, FogType type)
        {
            this.density = density;
            fogType = type;
        }
        
        public FogPostEffect(FogType type)
        {            
            fogType = type;
        }

        #region IPostEffect Members
               
        Effect effect = null;
        private Color fogColor = new Color(0.5f,0.5f,0.5f);
        private FogType fogType;
        private float density = 0.001f;

        /// <summary>
        /// Default 0.001f;
        /// </summary>
        public float Density
        {
            get { return density; }
            set { density = value; }
        }

        public FogType FogType
        {
            get { return fogType; }
            set { fogType = value; }
        }

        public Color FogColor
        {
            get { return fogColor; }
            set { fogColor = value; }
        }

        private float nearPlane = 1;

        public float NearPlane
        {
            get { return nearPlane; }
            set { nearPlane = value; }
        }
        private float farPlane = 1000;

        public float FarPlane
        {
            get { return farPlane; }
            set { farPlane = value; }
        }

        public void Draw(IRenderHelper rHelper, Microsoft.Xna.Framework.GameTime gt, Microsoft.Xna.Framework.Rectangle screenRec, IWorld world)
        {
                EngineStuff.GraphicsDevice.RenderState.DepthBufferWriteEnable = false;
                EngineStuff.GraphicsDevice.Clear(ClearOptions.DepthBuffer | ClearOptions.Target | ClearOptions.Stencil, Color.TransparentBlack, 1, 0);

                effect.Parameters["dz"].SetValue(density);
                effect.Parameters["depth"].SetValue(rHelper[PrincipalConstants.dephRT]);
                effect.Parameters["cena"].SetValue(rHelper[PrincipalConstants.CurrentImage]);
                effect.Parameters["InvertViewProjection"].SetValue(Matrix.Invert(world.ActiveCamera.View * world.ActiveCamera.Projection));
                effect.Parameters["halfPixel"].SetValue(EngineStuff.HalfPixel);
                effect.Parameters["cameraPosition"].SetValue(world.ActiveCamera.Position);                
                effect.Parameters["near"].SetValue(nearPlane);
                effect.Parameters["far"].SetValue(farPlane);
                effect.Parameters["fcolor"].SetValue(fogColor.ToVector3());
                rHelper.RenderTextureToFullScreenVertexPixel(effect);              
                    
        }

        public void init(EngineStuff engine)
        {
            effect = EngineStuff.InternalContentManager.GetAsset<Effect>("Fog");
            //effect = EngineStuff.CustomContentManager.GetAsset<Effect>("..\\Content\\Effects\\Fog");
            this.engine = engine;

            switch (fogType)
            {
                case FogType.LINEAR:
                    effect.CurrentTechnique = effect.Techniques["FogShader"];
                    break;
                case FogType.EXPONENCIAL:
                    effect.CurrentTechnique = effect.Techniques["FogExponencialShader"];
                    break;
                case FogType.EXPONENCIALSQUARED:
                    effect.CurrentTechnique = effect.Techniques["FogExponencialSquaredShader"];
                    break;
                default:
                    break;
            }
        }

        EngineStuff engine;

        #endregion
    }
}
